package com.chj.comp.common.mqadmin.controller;

import com.chj.comp.common.core.util.Result;
import com.chj.comp.common.security.annotation.NoAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import com.chj.comp.common.core.entity.MqMessageInfo;
import com.chj.comp.common.mqadmin.service.IMqMessageInfoService;

/**
 * <p>
 * rabbitmq消息信息表 前端控制器
 * </p>
 *
 * @author cuihui
 * @since 2022-06-14
 */
@RestController
@RequestMapping("/mqMessageInfo")
@Api(tags = "MqMessageInfo接口")
public class MqMessageInfoController {

    @Autowired
    private IMqMessageInfoService mqMessageInfoService;

    /**
     * 保存消息
     * @param  mqMessageInfo  实体
     * @return R<Boolean>
     */
    @PostMapping("/addMqMessageInfo")
    @ApiOperation(value = "保存消息")
    // 免鉴权注解
    @NoAuth
    public Result addMqMessageInfo(@RequestBody MqMessageInfo mqMessageInfo) {
        try {
            return mqMessageInfoService.addMqMessageInfo(mqMessageInfo);
        } catch (Exception e) {
            return  Result.failed(e.getMessage());
        }
    }

    /**
     * 更新消息状态
     * @param  messageId  消息id
     * @param  status  消息状态
     * @return R<Boolean>
     */
    @GetMapping("/updateMqMessageStatus")
    @ApiOperation(value = "更新消息状态")
    // 免鉴权注解
    @NoAuth
    public Result updateMqMessageStatus(@ApiParam(value = "消息id", required = true) @RequestParam(value = "messageId") String messageId,
                                        @ApiParam(value = "状态", required = true) @RequestParam(value = "status") String status) {
        try {
            return mqMessageInfoService.updateMqMessageStatus(messageId, status);
        } catch (Exception e) {
            return  Result.failed(e.getMessage());
        }
    }

    /**
     * 判断消息是否被消费
     * @param  messageId  消息id
     * @return R<Integer>
     */
    @GetMapping("/judgeMqMessageStatus/{messageId}")
    @ApiOperation(value = "判断消息是否被消费")
    // 免鉴权注解
    @NoAuth
    public Result<Integer> judgeMqMessageStatus(@ApiParam(value = "消息id", required = true) @PathVariable(value = "messageId") String messageId) {
        try {
            return mqMessageInfoService.judgeMqMessageStatus(messageId);
        } catch (Exception e) {
            return  Result.failed(e.getMessage());
        }
    }

    /**
     * 消息发送次数+1
     * @param  messageId  消息id
     * @return R<Boolean>
     */
    @GetMapping("/addMqMessageSendNum/{messageId}")
    @ApiOperation(value = "消息发送次数+1")
    // 免鉴权注解
    @NoAuth
    public Result addMqMessageSendNum(@ApiParam(value = "消息id", required = true) @PathVariable(value = "messageId") String messageId) {
        try {
            return mqMessageInfoService.addMqMessageSendNum(messageId);
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.failed(e.getMessage());
        }
    }
}
